<?php

class Categorys extends Admin_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('pagination');
        $this->load->model('category_m');
        // if(!$this->auth->is_admins()){
        // 	redirect(site_url('user/login'));
        // }
    }

    public function flist()
    {
        $data['title']    = '产品类型';
        $data['siderbar'] = 'flist';
        $data['submenu']  = 'categorys/flist';
        $pid              = 0;
        $data['cates']    = $this->category_m->get_cates_by_pid($pid);
        $this->load->view('categorys/category_list', $data);
    }

    public function del($id)
    {
        $data['title'] = '删除产品类型';
        $this->category_m->del_cate($id);
        show_message('删除产品类型成功！', site_url(X_URL . 'categorys/flist'), 1);
    }

    private function data_post($arr)
    {
        $str = [
            'pid'           => 0,
            'category_name' => $this->input->post('category_name'),
            'category_tax'  => $this->input->post('category_tax'),
            'order'         => $this->input->post("order"),
            'all_category'  => $this->input->post("all_category"),
        ];
        return $str;
    }

    public function add()
    {
        $data['title'] = '添加产品类型';

        if ($_POST) {
            $str = $this->data_post($_POST);//引用
            $this->category_m->add_cate($str);
            show_message('添加产品类型成功', site_url(X_URL . 'categorys/flist'), 1);
        }
        $this->load->view('categorys/category_add', $data);
    }

    public function edit($id)
    {
        $data['title'] = '修改产品类型';

        if ($_POST && $id) {
            $str = $this->data_post($_POST);//引用
            // var_dump($str);exit;
            if ($this->category_m->update_cate($id, $str)) {
                show_message('修改产品类型成功', site_url(X_URL . 'categorys/flist'), 1);
            } else {
                show_message('产品类型未做修改', site_url(X_URL . 'categorys/flist'));
            }
        }

        $cateinfo         = $this->category_m->get_category_by_id($id);
        $data['cateinfo'] = $cateinfo;
        $this->load->view('categorys/category_edit', $data);
    }

    public function mlist($all_category)
    {
        $data['title'] = '产品类型排序';
        $pid           = 0;

        $data['cates']        = $this->category_m->get_cates_by_all_category($all_category);
        $web_category         = $this->web_category;
        $web_category_txt     = $this->web_category_txt;
        $data['web_category'] = array_combine($web_category, $web_category_txt);
        $this->load->view('categorys/category_list', $data);
    }
}